package com.jinzihan.utils;

import com.jinzihan.entity.Classroom;
import org.apache.commons.beanutils.BeanUtils;

import javax.servlet.http.HttpServletRequest;
import java.lang.reflect.InvocationTargetException;
import java.util.Map;

public class WebUtils {
    public static <T> T copyParamToBean(Map value,T bean){
        try {
            BeanUtils.populate(bean,value);
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
        return bean;
    }

    public static int parseInt(String strInt,int defaultValue){
            try {
                return Integer.parseInt(strInt);
            } catch (NumberFormatException e) {

            }
        return defaultValue;
    }

    public static Classroom copyToClassroom(HttpServletRequest req){
        String name = req.getParameter("name");
        int capacity = WebUtils.parseInt(req.getParameter("capacity"),0);
        String strMedium = req.getParameter("medium");
        int medium;
        if (strMedium.equals("有")){
            medium=1;
        }else {
            medium=0;
        }
        int type;
        String strType = req.getParameter("type");
        if (strType.equals("阶梯教室")){
            type=1;
        }else {
            type=0;
        }
        int status;
        String strStatus = req.getParameter("status");
        if (strStatus.equals("空闲中")){
            status=0;
        }else if (strStatus.equals("使用中")){
            status=2;
        }else {
            status=1;
        }
        Classroom classroom = new Classroom(null,name,capacity,medium,type,status);
        return classroom;
    }
}
